package com.yc.rpc.core;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cglib.proxy.Proxy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * @author 冉野 E-mail:yangtianrui135@163.com
 * @version 创建时间：2021/7/13 4:36 PM
 */
public class YcRpcFactoryBean implements FactoryBean<Object>, ApplicationContextAware {

    public YcRpcFactoryBean() {

    }

    ApplicationContext context;

    private Class<?> type;

    public Class getType() {
        return type;
    }

    public void setType(Class type) {
        this.type = type;
    }

    @Override
    public Object getObject() throws Exception {

       return Proxy.newProxyInstance(
                ReflectionRpc.class.getClassLoader(), // 传入ClassLoader
                new Class[] { type }, // 传入要实现的接口
                new YcRpcInvocationHandler(context)); // 传入处理调用方法的InvocationHandler

    }

    @Override
    public Class<?> getObjectType() {
        return type;
    }



    @Override
    public boolean isSingleton() {
        return true;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
